import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/Login.vue'
import Frame from '../views/frame/Frame.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/frame',
    component: Frame,
    children: [      
      {
        path: '',
        name: 'Customer',
        component: () => import('../views/customer/Customer.vue')
      },
      {
        path: 'test',
        component: Login
      },
      {
        path: 'example/validate-form',
        name: 'ValidateFormTest',
        component: () => import('../views/example/ValidateForm.vue')
      }
    ]
  },
  {
    path: '*',
    component: Login
  }  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {  
    let loginUser = localStorage.getItem('loginUser')
    if (loginUser === null){
      next('/login')
    }
  }

  next()
})

export default router
